package top.scauwlt;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import top.scauwlt.annotation.RpcScan;
import top.scauwlt.config.RpcConfig;
import top.scauwlt.factory.SingletonFactory;
import top.scauwlt.remoting.transport.server.NettyRpcServer;

/**
 * @Author wmm
 * @Date 2022/7/28 21:59
 */
@Slf4j
public class RpcServerProvider implements CommandLineRunner {
    private final NettyRpcServer nettyRpcServer;

    public RpcServerProvider() {
        nettyRpcServer = SingletonFactory.getInstance(NettyRpcServer.class);
    }

    public RpcServerProvider(RpcProperties rpcProperties) {
        this();
        log.info("{}", rpcProperties);
        if (rpcProperties == null) {
            return;
        }
        if (rpcProperties.getPort() != null) {
            RpcConfig.getInstance().setNettyPort(rpcProperties.getPort());
        }
        if (rpcProperties.getCompress() != null) {
            RpcConfig.getInstance().setCompressType(rpcProperties.getCompress());
        }
        if (rpcProperties.getSerialization() != null) {
            RpcConfig.getInstance().setSerializationType(rpcProperties.getSerialization());
        }

    }


    @Override
    public void run(String... args) throws Exception {
        log.info("启动 rpc服务端...");
        new Thread(() -> {
            nettyRpcServer.start();
        }).start();
    }
}
